package com.easy.controller;

import com.easy.bean.Order;
import com.easy.common.CommonResult;
import com.easy.dao.IOutOrderDao;
import com.easy.service.IOutOrderService;
import com.easy.util.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("outorder")
public class OutOrderController {
    private static final Logger logger = LoggerFactory.getLogger(InOrderController.class);

    @Autowired
    private IOutOrderService is;
    @Autowired
    private IOutOrderDao dao;

    // 导出excel 出库表格
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportOrdersToExcel() {
        try {
            List<Order> orders = is.getOutOrder();
            byte[] excelBytes = ExcelUtil.exportOutOrderListToExcel(orders);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            // 确保文件名中没有不允许的字符，并且正确编码以避免出现下载时的乱码问题
            headers.setContentDispositionFormData("attachment", "out_orders.xlsx");

            return ResponseEntity.ok()
                    .headers(headers)
                    .body(excelBytes);

        } catch (Exception e) {
            logger.error("Error exporting orders to Excel", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @GetMapping("/{out_id}")  // 将路径改为 {out_id}
    public CommonResult getOrdersByOutId(@PathVariable("out_id") String out_id) {  // 统一为 out_id
        List<Order> orders = is.getOrdersByOutId(out_id);
        return CommonResult.success(orders);
    }
    @GetMapping("/all")
    public CommonResult getIOutOrder() {
        List<Order> list = is.getOutOrder();
        return CommonResult.success(list);
    }

    @PostMapping("/add")
    public CommonResult addOutOrder(@RequestBody Order order){
        int count = is.addOutOrder(order);
        if(count>0) {
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }    }

    @PutMapping("/edit/{id}")
    public CommonResult editOutOrder(@PathVariable int id, @RequestBody Order order){
        order.setId(id);  // 将URL中的id设置到User对象中
        int count = is.editOutOrder(order);
        if(count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.fail();
        }
    }

    @DeleteMapping("/del/{id}")
    public CommonResult delOutOrder(@PathVariable int id) {
        int count = is.delOutOrder(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.fail();
        }
    }

    //  查询商品名或id
    @GetMapping("/search")
    public CommonResult searchOutOrder(@RequestParam String keyword){
        List<Order> inventories = is.searchOutOrder(keyword);
        return CommonResult.success(inventories);
    }

    //   分页
    @GetMapping("/paged")
    public CommonResult getPagedOutOrder(@RequestParam int page, @RequestParam int size) {
        List<Order> list = is.getPagedOutOrder(page, size);
        int total = is.getTotalOutOrderCount();
        return CommonResult.success(list, total);
    }
}
